/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ddnsserver;
import java.util.Vector;
/**
 *
 * @author Do Viet Trung
 */
public class Package
{
    public enum CODE {
        LOGIN,//client gửi username và password
        HELLO,
        HOST,//client gửi domain
        LOGOUT,//client logout
        UPDATE_IP,//update ip
        NOLOGIN,//ko dc login
        OPERATE_SUCCESS,//thao tac thanh cong
        OPERATE_FAILURE,//thao tac that bai
        OPERATE_PERMISSON_OK,//dc thuc hien thao tac nay
        OPERATE_PERMISSON_NOT_OK,//khong duoc thuc hien thao tac nay
        UNKNOW,
        INVALID_CMD;//lenh khong hop le
    };
    public enum TYPE {
        MESSAGE_OF_CLIENT,
        MESSAGE_OF_SERVER,
        UNKNOW;
    };

    private String Username,
                    Revision,
                    Message;
    private CODE Code;
    private TYPE Type;
    private static String pattern = "-";

    public Package()
    {
        Username = "";
        Revision = "";
        Message = "";
        Code = CODE.UNKNOW;
        Type = TYPE.UNKNOW;
    }
    public Package(String txtFormat, TYPE type)
    {
        Type = type;
        Vector<String> pack = JProcess.parseString(txtFormat, '-');
        if(pack.size()  == 4)
        {
            Username = pack.elementAt(0);
            Revision = pack.elementAt(2);
            Message = pack.elementAt(3);
            Code = switchCode(pack.elementAt(1), type);
        }
    }
    public String getUsername()
    {
        return Username;
    }
    public String getMessage()
    {
        return Message;
    }
    public String getRevision()
    {
        return Revision;
    }
    public CODE getCode()
    {
        return Code;
    }
    public void setUsername(String usr)
    {
        if(usr != null)
        {
            Username = usr;
        }
    }
    public void setMessage(String msg)
    {
        if(msg != null)
        {
            Message = msg;
        }
    }
    public void setRevision (String rev)
    {
        if(rev != null)
        {
            Revision = rev;
        }
    }
    public void setCode(CODE c)
    {
        Code = c;
    }
    public void addRevision(int rev)
    {
        int r = Integer.valueOf(Revision);
        Revision = String.valueOf( r + rev);
    }
    public String createPackage(String Message,CODE code)
    {
        String res = "";
        switch(code)
        {
            case HELLO:
            {
                res += Username + pattern + 200 + pattern + Revision + pattern + Message;

                break;
            }
            case LOGIN:
            {
                res += Username + pattern + 201 + pattern + Revision + pattern + Message;
                break;
            }
            case NOLOGIN:
            {
                res += Username + pattern + 202 + pattern + Revision + pattern + Message;
                break;
            }

            case OPERATE_SUCCESS:
            {
                res += Username + pattern + 203 + pattern + Revision + pattern + Message;
                break;
            }
            case OPERATE_FAILURE:
            {
                res += Username + pattern + 204 + pattern + Revision + pattern + Message;
                break;
            }
            case OPERATE_PERMISSON_OK:
            {
                res += Username + pattern + 205 + pattern + Revision + pattern + Message;
                break;
            }
            case OPERATE_PERMISSON_NOT_OK:
            {
                res += Username + pattern + 206 + pattern + Revision + pattern + Message;
                break;
            }
            case HOST:
            {
                res += Username + pattern + 207 + pattern + Revision + pattern + Message;
                break;
            }
            case INVALID_CMD:
            {
                res += Username + pattern + 298 + pattern + Revision + pattern + Message;
                break;
            }
            case LOGOUT:
            {
                res += Username + pattern + 299 + pattern + Revision + pattern + Message;
                break;
            }

        }
        if(res != "")
        {
            addRevision(1);
        }
        return res;
    }
    private CODE switchCode(String Code, TYPE type)
    {
        switch(type)
        {
            case MESSAGE_OF_CLIENT:
            {
                if(Code.startsWith("100"))
                {
                    return CODE.LOGIN;
                }
                if(Code.startsWith("101"))
                {
                    return CODE.HOST;
                }
                if(Code.startsWith("102"))
                {
                    return CODE.UPDATE_IP;
                }
                if(Code.startsWith("199"))
                {
                    return CODE.LOGOUT;
                }
                break;
            }
            case MESSAGE_OF_SERVER:
            {
                if(Code == "200")
                {
                    return CODE.HELLO;
                }
                if(Code == "201")
                {
                    return CODE.LOGIN;
                }
                if( Code == "202")
                {
                    return CODE.NOLOGIN;
                }
                if(Code == "298")
                {
                    return CODE.INVALID_CMD;
                }
                if(Code == "299")
                {
                    return CODE.LOGOUT;
                }
                break;
            }
        }
        return CODE.UNKNOW;
    }
}

